/**
 * Created by TianJing on 2017/3/20.
 */
(function() {
    'use strict';

    var checkedObject = {};
    /**
     * 中介公司  列表初始化设置
     * @type {Object}
     */
    var settings = {
        // "sAjaxSource": window.globalHost + "/agent-sys/admin/agencyInfo/index.do",
        "sAjaxSource": window.globalHost + common.storeListPort,
        "aoColumns": [{
            "mDataProp": function(obj) {
                var checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" data-name="'+obj.storeName+'" data-shortName="'+obj.agencyName+'">';
                if (checkedObject[obj.id]) {
                    checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked" data-name="'+obj.storeName+'" data-shortName="'+obj.agencyName+'">';
                }
                return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
            },
            "sTitle": "",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": function(obj) {
                return "<a href='../submenu.html?key=agencyStore&id=" + obj.id + "'>" + obj.storeName + "</a>"
            },
            "sTitle": "门店名称",
            "sDefaultContent": ""
        },{
            "mDataProp": function(obj) {
                return "<a href='../submenu.html?key=agencyStore&id=" + obj.id + "'>" + obj.companyName + "</a>"
            },
            "sTitle": "中介公司全称",
            "sDefaultContent": ""
        },{
            "mDataProp":"storeAddress",
            "sTitle": "门店地址",
            "sDefaultContent": ""
        }, {
            "mDataProp": "contactName",
            "sTitle": "主要联系人",
            "sDefaultContent": ""
        },{
            "mDataProp": "contactTel",
            "sTitle": "主要联系人电话",
            "sDefaultContent": ""
        }, {
            "mDataProp": "createTime",
            "sTitle": "录入时间",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.data-list-search",aoData);
        }
    };
    // $("body").on("click",".allClick",function(ev){
    //     var list = $("body").find("input[type='checkbox']");
    //     list.each(function(i,I){
    //         $(I).trigger('click')
    //     })
    // });
    var options = $.extend(true, {}, _dataTableSettings, settings);
    /**
     * 实例化中介公司列表插件
     * @type {[type]}
     */
    var $currentDataTable=$("#data-list");
    var oTable = $currentDataTable.dataTable(options);
    searchList.getDataList(oTable);
    searchList.resetSearch(oTable);
    /**
     * 绑定数据列表中的多选框
     * @param  {[type]} ev){                     var checkItem [description]
     * @return {[type]}       [description]
     */
    $currentDataTable.on("change", ".checkbox", function(ev) {
        var checkItem = $(this).find("input[type='checkbox']");
        var checkId = checkItem.attr("id").replace('checkbox-', '');
        var checkName=checkItem.attr("data-name");
        var shortName=checkItem.attr("data-shortName");
        if (checkItem[0].checked) {
            // checkedObject[checkId] = true;
            checkedObject[checkId]= {
                id:checkId,
                name:checkName,
                shortName:shortName
            };
        } else {
            delete checkedObject[checkId];
        }
        ev.stopPropagation();
    });
    //合并操作
    $("button.margeStore").on("click", function(ev) {
        var contentList=[];
        var idList=[];
        $.each(checkedObject,function (key, val) {
            idList.push(key);
            // contentList.push('<li data-value="'+key+'">'+val.name+'</li>');
            contentList.push({
                value:key,
                text:val.name
            });
        });
        if(idList.length<1){
            layer.alert("请选择需要合并的中介门店!");
            return;
        }else if(idList.length<2){
            layer.alert("合并的中介门店少于2个!");
            return;
        }
        var sendData={};
        common.openTempDialog({
            title: '合并同类中介门店',
            area:['640px', '300px'],
            content: '<div id="mergeStore">' +
                        '<p class="text-danger text-center" style="margin:5px">请选择主要的中介门店!!</p>' +
                        '<ul class="husky-radio clearfix" name="mainId"></ul>' +
                    '</div>',
            sendData:sendData,
            FailedMsg:"请选择主要中介门店",
            contentOnLoad:function (layerDocument) {
                //弹窗内控件加载初始化
                var $form=layerDocument.find("#mergeStore");
                window.myHusky= new Husky($form);
                myHusky.createRadio({
                    parent:$form.find(".husky-radio"),
                    configJson:contentList,
                    callback:function (idx) {

                    }
                });
            },
            beforeSend:function (layerDocument) {
                sendData.mainId=myHusky.getJsonByForm().mainId;
                sendData.ids=idList.join(",");
                return sendData['mainId'];
            }

        });
        ev.stopPropagation();
    });

    /**
     * [新增中介公司弹框，common.openInsertDialog 是一个公共的弹框。专门用户处理 "新增页面弹框"]
     * @param  {[type]} ev){                     common.openInsertDialog({            'url':'./agency/insertHouse.html',            'title':'添加中介公司'        })    } [description]
     * @return {[type]}       [description]
     */
    $("button.insertStore").on('click', function(ev) {
        common.openInsertDialog({
            // 'url': './agency/insertAgency.html',
            'url': './agencyManage/insertStore.html',
            'title': '添加中介门店',
            'sAjaxSource': window.globalHost + common.storeAddPort,
            'area': ['1000px', '600px'],
            ok: function() {
                //  新增信息成功之后，刷新列表页面
                oTable.fnClearTable();
            },
            cancel: function() {}
        });
        ev.stopPropagation();
    });

}).call(this);